package com.xj.zhihudemo.ui.listener;

import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

/**
 * Created by Administrator on 2016/10/13.
 */

public class RecyclerViewLoadMoreListener extends RecyclerView.OnScrollListener {
    private final LinearLayoutManager mLinearLayoutManager;
    private final OnLoadMoreListener mOnLoadMoreListener;
    private final int limit;

    public RecyclerViewLoadMoreListener(
            @NonNull LinearLayoutManager linearLayoutManager,
            @NonNull OnLoadMoreListener onLoadMoreListener,
            int limit) {
        super();
        this.mLinearLayoutManager = linearLayoutManager;
        this.mOnLoadMoreListener = onLoadMoreListener;
        this.limit = limit;
    }

    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        if (newState == RecyclerView.SCROLL_STATE_IDLE &&
                mLinearLayoutManager.findLastVisibleItemPosition() == mLinearLayoutManager.getItemCount() - 1) { // 向下滑动，判断最后一个item是不是显示中
            mOnLoadMoreListener.onLoadMore();
        }
    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
//        lastVisibleItem =linearLayoutManager.findLastVisibleItemPosition();
    }

    public interface OnLoadMoreListener {
        void onLoadMore();

    }
}
